#include "MaterialSplitVectorNodeModel.h"

#include "MaterialGraph/Pin/MaterialGraphPinModel.h"
#include "Object/NewObject.h"

void RMaterialSplitVectorNodeModel::FirstTimeInitializeObject()
{
    //add X output pin
    auto XOutputPin = NewObject<RMaterialGraphPinModel>(this);
    XOutputPin->ValueType = EMaterialGraphValueType::Vector1D;
    XOutputPin->bIsOutput = true;
    Outputs.Add(XOutputPin);

    //add Y output pin
    auto YOutputPin = NewObject<RMaterialGraphPinModel>(this);
    YOutputPin->ValueType = EMaterialGraphValueType::Vector1D;
    YOutputPin->bIsOutput = true;
    Outputs.Add(YOutputPin);

    //add Z output pin
    auto ZOutputPin = NewObject<RMaterialGraphPinModel>(this);
    ZOutputPin->ValueType = EMaterialGraphValueType::Vector1D;
    ZOutputPin->bIsOutput = true;
    Outputs.Add(ZOutputPin);

    //add 3D InputPin
    auto InputPin3D = NewObject<RMaterialGraphPinModel>(this);
    InputPin3D->ValueType = EMaterialGraphValueType::Vector3D;
    InputPin3D->bIsOutput = false;
    Inputs.Add(InputPin3D);

    //add 2D InputPin
    auto InputPin2D = NewObject<RMaterialGraphPinModel>(this);
    InputPin2D->ValueType = EMaterialGraphValueType::Vector2D;
    InputPin2D->bIsOutput = false;
    Inputs.Add(InputPin2D);

}

CString RMaterialSplitVectorNodeModel::GenerateExpression()
{
    CString Result;
    if(bSplit2D)
    {
        if(Outputs[0]->HasValidLink())
        {
            Result+=
                U"float "+Outputs[0]->UniqueOutputPinName+U" = "+Inputs[1]->GetValueExpression()+U".x;\n";
        }

        if(Outputs[1]->HasValidLink())
        {
            Result+=
                U"float "+Outputs[1]->UniqueOutputPinName+U" = "+Inputs[1]->GetValueExpression()+U".y;\n";
        }

        if(Outputs[2]->HasValidLink())
        {
            Result+=
                U"float "+Outputs[2]->UniqueOutputPinName+U" = 0.0;\n";
        } 
    }
    else
    {
        if(Outputs[0]->HasValidLink())
        {
            Result+=
                U"float "+Outputs[0]->UniqueOutputPinName+U" = "+Inputs[0]->GetValueExpression()+U".x;\n";
        }

        if(Outputs[1]->HasValidLink())
        {
            Result+=
                U"float "+Outputs[1]->UniqueOutputPinName+U" = "+Inputs[0]->GetValueExpression()+U".y;\n";
        }

        if(Outputs[2]->HasValidLink())
        {
            Result+=
                U"float "+Outputs[2]->UniqueOutputPinName+U" = "+Inputs[0]->GetValueExpression()+U".z;\n";
        } 
    
    }

    return Result;
}

void RMaterialSplitVectorNodeModel::SetSplit2D(bool bInSplit2D)
{ 
    bSplit2D = bInSplit2D; 

    //update input pin
    if(bSplit2D)
    {
        Inputs[0]->bIsHidden = true;
        Inputs[1]->bIsHidden = false;
    }
    else
    {
        Inputs[0]->bIsHidden = false;
        Inputs[1]->bIsHidden = true;
    }

}